<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Field as fieldModel;
use think\facade\Db;
use think\facade\View;

class Field extends Base{
    // 列表
    public function index(){
    	$keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];
		$rs = fieldModel::index($this->lang,$keyword,$order);
		
		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());
		
        return view();
    }

    // 添加编辑
    public function edit($id=0){
		if(!request()->isPost()){
            $rs = fieldModel::find($id);
            View::assign('rs',$rs);
            View::assign('fieldtype',input('fieldtype'));
            
			return view();
        }else{
			$fieldlabel = input('fieldlabel');
			// 标签规则验证
			$preg = '/^(?!_)(?!.*?_$)[a-z_]{2,20}$/';
			if(!preg_match($preg,$fieldlabel)){
				return json(['code' => 1001, 'msg' => lang('y_fieldlabel'), 'data' => array('fieldlabel'=>$fieldlabel)]);
			}
			// 标签唯一性验证
			$fieldNum = fieldModel::where(['lang'=>$this->lang,'fieldlabel'=>$fieldlabel])->count();
			if(($fieldNum > 1 and !empty($id)) or ($fieldNum > 0 and empty($id))){
				return json(['code' => 1002, 'msg' => lang('y_fieldlabel'), 'data' => array('id'=>$id)]);
			}
				
			try {
	            fieldModel::edit($id);
	            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
	        } catch (Exception $e) {
	            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
	        }
        }		
	}
	
	// 单条删除
    public function delete_one(){
	    $id = input('id');
        $fieldRs = fieldModel::find($id);

        // 含有禁止删除的信息
        $nodeleteidArr = explode(',', config('app.no_delete_fieldid'));
        if(in_array($id, $nodeleteidArr)){
        	return json(['code' => 1001, 'msg' => lang('c_nodeleteid_error'), 'data' => array('id'=>$id)]);
        }

        // 删除对应的图片
        if(config('app.deletefile') == 1 and ($fieldRs['fieldtype'] == 'thumb' or $fieldRs['fieldtype'] == 'file')){
		    $thumb = substr($fieldRs['fieldvalue'],1);
            @unlink($thumb);
        }

		try {
            $fieldRs->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	/* 批量删除，返回值：
    @ error:no_check 未勾选数据
    @ success 成功
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
			return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
		}

		// 含有模块禁止删除的信息
        $nodeleteidArr = explode(',', config('app.no_delete_fieldid'));
        foreach ($selectid as $infoid) {
            if(in_array($infoid, $nodeleteidArr)){
            	return json(['code' => 1002, 'msg' => lang('c_nodeleteid_error'), 'data' => array('infoid'=>$infoid)]);
            }
            break;
        }
		
		$deletefile = config('app.deletefile');
		foreach($selectid as $i => $rs){
		    $id = $selectid[$i];
			$fieldRs = fieldModel::find($id);
			// 删除对应的图片
            if($deletefile == 1 and ($fieldRs['fieldtype'] == 'thumb' or $fieldRs['fieldtype'] == 'file')){
		        $thumb = substr($fieldRs['fieldvalue'],1);
                @unlink($thumb);
            }
	    }

		try {
            Db::name('field')->delete($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	// 批量更新
    public function update_all(){
		$id = input('id/a'); $fieldtext = input('fieldtext/a'); $sequence = input('sequence/a');
		foreach($id as $i => $rs){
            $data[] = ['id'=>$id[$i],'fieldtext'=>$fieldtext[$i],'sequence'=>$sequence[$i]];
        }
        
		try {
            $field = new fieldModel;
			$field->saveAll($data,true);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}